{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f802e64d-4eaa-445d-a48a-1042a91bc394",
   "metadata": {
    "tags": []
   },
   "source": [
    "# console端RAG问答助手\n",
    "\n",
    "## 目标\n",
    "使用console端RAG应用，对用户的请求进行回答。\n",
    "\n",
    "## 准备工作\n",
    "### 平台注册\n",
    "\n",
    "1.先在appbuilder平台注册，获取token\n",
    "\n",
    "2.安装appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2939356f-61c2-42e9-9e0c-fc6729c193f6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4ccff03b-1567-4e8b-8e1f-9a5032690406",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-29T08:53:09.839677Z",
     "start_time": "2024-01-29T08:53:09.836602Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init done\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "#  设置环境变量\n",
    "#  请前往千帆AppBuilder官网创建密钥，流程详见：https://cloud.baidu.com/doc/AppBuilder/s/Olq6grrt6#1%E3%80%81%E5%88%9B%E5%BB%BA%E5%AF%86%E9%92%A5\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"...\"\n",
    "\n",
    "print(\"init done\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeb2fa55-075f-48df-a9fb-8b40d9900684",
   "metadata": {},
   "source": [
    "## 开发过程"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e77c402f3ec3e40",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 获取线上已有RAG应用的ID\n",
    "线上已有RAG应用ID，可在[console](https://console.bce.baidu.com/ai_apaas/app)端查看，注意，完成应用的创建后，需要点击立即使用，发布应用后，才可被SDK调用，示例,\n",
    "\n",
    "<img width=\"1536\" alt=\"image\" src=\"./image/rag示例.png\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41559341-fd7a-478c-a08b-1477d79e9d41",
   "metadata": {},
   "outputs": [],
   "source": [
    "import appbuilder\n",
    "\n",
    "# 线上RAG应用ID，可在console端查看\n",
    "app_id = \"...\"\n",
    "conversation_id = \"...\" # 会话ID，可选参数，不传默认新建会话\n",
    "rag_app = appbuilder.console.RAG(app_id)\n",
    "query = \"中国的首都在哪里\"\n",
    "answer = rag_app.run(appbuilder.Message(query)) # 新建对话\n",
    "print(answer.content)\n",
    "conversation_id = answer.conversation_id # 获取会话ID，可用于下次会话\n",
    "print(conversation_id)\n",
    "query = \"它有哪些旅游景点\"\n",
    "answer = rag_app.run(appbuilder.Message(query), conversation_id) # 接上次对话\n",
    "print(answer.content)\n",
    "print(answer.extra)  # 获取结果来源"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
